package com.itheima.testservlet;

import com.itheima.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    //登录方法
    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用service中的登录方法
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        System.out.println(email+password);
        User user=userService.login(email,password);

        //对user进行判断
        if(user!=null){//邮箱和密码在数据库匹配成功，登录成功
            req.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(req,resp);

        }else {
            req.getRequestDispatcher("/WEB-INF/pages/test.jsp").forward(req,resp);
        }
    }
    //遍历
    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> list = userService.findAll();
        req.setAttribute("userList",list);
        req.getRequestDispatcher("/WEB-INF/pages/list.jsp").forward(req,resp);
    }
}
